package model

import (

	"fmt"
)

func GetExistAdmin(name string) (i,n ,p string) {
	result,err:=DB.Query("select * from admin where admin_name = $1 ;",name)
	if err!=nil{
		fmt.Println("GetExistUser err:",err)
	}

	var id,dbname,password string
	for result.Next() {

		err = result.Scan(&id,&dbname,&password)
	}


	return id,dbname,password
}
func GetExistStudent(studentID,name,college,specialize,class string)  bool {
	result,err:=DB.Query("select * from student where student_id = $1 ;",studentID)
	if err!=nil{
		fmt.Println("GetExistUser err:",err)
	}

	var id,sid,n,coll,specia,ca string

	for result.Next() {

		err = result.Scan(&id,&sid,&n,&coll,&specia,&ca)
	}
	if len(id)<1{
		InsertUser(studentID,name,college,specialize,class)
		return true
	}
	if n!=name || coll !=college||specia!=specialize||class!=ca{
		return false
	}

	return true
}

func InsertUser(studentID,name,college,specialize,class string) bool {
	stmt, err := DB.Prepare("Insert into student(student_id,student_name,college,specialize,student_class) values($1,$2,$3,$4,$5)")
	if err != nil {
		fmt.Println("Some amazing wrong happens in preparation for the insert.")
		return false
	}
	defer stmt.Close()
	res, err := stmt.Exec(studentID,name,college,specialize,class)
	if err != nil {
		fmt.Println("Some amazing wrong happens in the process of doInsertOne.")
		return false
	}
	num, err := res.RowsAffected()
	if err != nil {
		fmt.Println("Some amazing wrong happens in the affected fo deletion.")
		return false
	}
	fmt.Println("Successfully add ", num)
	return true
}
